/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmNhapSanPham.java
 *
 * Created on Aug 22, 2012, 3:06:32 PM
 */
package gui;

import java.awt.*;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;
import dao.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import pojo.*;
import javax.swing.table.DefaultTableModel;
import org.hibernate.LazyInitializationException;
/**
 *
 * @author dtth
 */
public class frmNhapSanPham extends javax.swing.JFrame {

    // Vùng khai báo biến
    private static Khosanpham pojoKhoSanPham = new Khosanpham();
    private static Cuahang pojoCuaHang = new Cuahang();
    private static Danhmuc pojoDanhMuc = new Danhmuc();
    private static Donvitinh pojoDonViTinh = new Donvitinh();
    private static Nhanvien pojoNhanVien = new Nhanvien();
    private static Sanpham pojoSanPhamVuaThem = new Sanpham();
    private boolean isMaDanhMucSelected = false;
    private boolean isDonViTinhSelected = false;
    private DefaultTableModel TableModel;
    private DefaultTableModel TableModelVuaThem;
    Vector rowHeader = new Vector();
    Vector rowHeaderVuaThem = new Vector();
    private static Integer iSoLuongSanPham = 0;
    private static int iSoLuong = 1;
    
    // Kết thúc Vùng khai báo biến
    
    // Vùng cài đặt các phương thức
    public void setInfor_Temp() {
        pojoKhoSanPham.setMakhosanpham(4);
        pojoKhoSanPham.setSoluongton(100);
        
        pojoCuaHang.setMacuahang(1);
        pojoCuaHang.setChucuahang("Ông A");
        pojoCuaHang.setDiachi("Quận Bình Thạnh");
        pojoCuaHang.setTencuahang("Simanchao");
        
        pojoNhanVien = new Nhanvien(1, "Lâm Văn Sơn");
    }
    public static void setInformations(Nhanvien prNV, Cuahang prCH, Khosanpham prKSP) {
        pojoNhanVien = prNV;
        pojoCuaHang = prCH;
        pojoKhoSanPham = prKSP;
    }
    public void CreateHeaderTableSanPham() {
        try {
            TableModel = new DefaultTableModel(new Vector(),0);
            rowHeader.addElement("STT");    
            rowHeader.addElement("Mã SP");    
            rowHeader.addElement("Tên SP");    
            rowHeader.addElement("Đơn Giá");    
            rowHeader.addElement("Số Lượng Tồn");   
            rowHeader.addElement("ĐVT");    
            rowHeader.addElement("Danh Mục");
            
            TableModel = new DefaultTableModel(rowHeader, 0);
            jTableSanPham.setModel(TableModel);
        } catch(Exception ex) {
            
        }
    }
    public void CreateHeaderTableSanPhamVuaThem() {
        try {
            TableModelVuaThem = new DefaultTableModel(new Vector(),0);
            rowHeaderVuaThem.addElement("STT");    
            rowHeaderVuaThem.addElement("Mã SP");  
            rowHeaderVuaThem.addElement("Tên SP");    
            rowHeaderVuaThem.addElement("Đơn Giá");    
            rowHeaderVuaThem.addElement("Số Lượng");   
            rowHeaderVuaThem.addElement("ĐVT");    
            rowHeaderVuaThem.addElement("Danh Mục");
            
            TableModelVuaThem = new DefaultTableModel(rowHeaderVuaThem, 0);
            jTableSanPhamVuaThem.setModel(TableModelVuaThem);
        } catch(Exception ex) {
            
        }
    }
    public void CreateTableSanPham() {
        try {
            for (int i = TableModel.getRowCount() - 1; i >= 0; i-- ){
                TableModel.removeRow(i);
            }
            
            Iterator<Sanpham> sanphams = pojoDanhMuc.getSanphams().iterator();
            int i = 1;
            while(sanphams.hasNext()) {
                Sanpham sp = sanphams.next();
                //Khosanpham ksp = KhoSanPhamDAO.layThongTinKhoSanPhamTheoMaSP(sp);
                Khosanpham ksp = new Khosanpham();
                Iterator<Khosanpham> khosanphams = sp.getKhosanphams().iterator();
                while(khosanphams.hasNext()) {
                    Khosanpham k = khosanphams.next();
                    int iMCH_pojo = pojoCuaHang.getMacuahang();
                    int iMCH_kho = k.getCuahang().getMacuahang();
                    if(iMCH_pojo == iMCH_kho) {
                        ksp = k;
                        break;
                    }
                } 
                Vector row = new Vector();
                row.addElement(String.valueOf(i).toString());
                row.addElement(sp.getMasanpham());
                row.addElement(sp.getTensanpham());
                row.addElement(sp.getDongia());
                
                row.addElement(ksp.getSoluongton().toString());
                Donvitinh dvt = sp.getDonvitinh();
                row.addElement(dvt);
                Danhmuc dm = sp.getDanhmuc();
                row.addElement(dm);
                TableModel.addRow(row);
                i++;
                //pojoSanPhamVuaThem = new Sanpham(sp.getMasanpham(), dm, dvt, sp.getTensanpham(), sp.getDongia(), null, null, null);
//                Iterator<Khosanpham> khos = sp.getKhosanphams().iterator();
//                while(khos.hasNext()) {
//                    Khosanpham ksp = khos.next();
//                    if(ksp.getMakhosanpham() == pojoKhoSanPham.getMakhosanpham()) {
//                        Vector row = new Vector();
//                        row.addElement(String.valueOf(i).toString());
//                        row.addElement(sp.getTensanpham());
//                        row.addElement(sp.getDongia());
//                        row.addElement("10");
//                        Donvitinh dvt = sp.getDonvitinh();
//                        row.addElement(dvt.getTendonvitinh());
//                        Danhmuc dm = sp.getDanhmuc();
//                        row.addElement(dm.getTendanhmuc());
//                        TableModel.addRow(row);
//                        i++;
//                    }
//                }
            }
            jTableSanPham.setModel(TableModel);
        } catch(LazyInitializationException ex) {
            System.out.println("Loi Lazy: " + ex.toString());
        } catch(Exception ex) {
            System.out.println("Exception: " + ex.toString());
        }
    }
    public void CreateTableSanPhamVuaThem(Sanpham sp, String strSoLuong) {
        try {
            for (int i = TableModelVuaThem.getRowCount() - 1; i >= 0; i-- ){
                int iMaSP = Integer.parseInt(jTableSanPhamVuaThem.getModel().getValueAt(i, 1).toString());
                if(iMaSP == sp.getMasanpham()){
                    Integer iSLSPTable = Integer.valueOf(jTableSanPhamVuaThem.getModel().getValueAt(i, 4).toString());
                    Integer iSLSP = iSLSPTable + Integer.parseInt(iSoLuongSanPham.toString());
                    jTableSanPhamVuaThem.getModel().setValueAt(iSLSP, i, 4);
                    return;
                }
            }

            Vector row = new Vector();
            row.addElement(String.valueOf(iSoLuong).toString());
            row.addElement(sp.getMasanpham());
            row.addElement(sp.getTensanpham());
            row.addElement(sp.getDongia());
            row.addElement(strSoLuong);
            row.addElement(pojoDonViTinh);
            row.addElement(pojoDanhMuc);
            TableModelVuaThem.addRow(row);
            iSoLuong++;
            jTableSanPhamVuaThem.setModel(TableModelVuaThem);
        } catch(LazyInitializationException ex) {
            System.out.println("Loi Lazy: " + ex.toString());
        } catch(Exception ex) {
            System.out.println("Exception: " + ex.toString());
        }
    }
    public void LoadDanhMucSanPham() {
        try {
            List<Danhmuc> listDM = DanhMucDAO.layDanhSachDanhMuc();
            for (int i = 0; i < listDM.size(); i++) {
                Danhmuc dm = listDM.get(i);
                jcbxDanhMucSP.addItem(dm);
            }
        
        
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, "Loi: " + ex.toString(), "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Kết thúc Vùng cài đặt các phương thức
    
    
    /** Creates new form frmNhapSanPham */
    public frmNhapSanPham() {
        initComponents();
        LoadDanhMucSanPham();
        CreateHeaderTableSanPham();
        CreateHeaderTableSanPhamVuaThem();
        setInfor_Temp();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane2 = new javax.swing.JDesktopPane();
        dateChooserPanel1 = new com.toedter.calendar.demo.DateChooserPanel();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtSoLuong = new javax.swing.JTextField();
        jcbxDanhMucSP = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jbtnHuy = new javax.swing.JButton();
        jbtnThem = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jlblDonGiaSP = new javax.swing.JLabel();
        jlblTenSP = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSanPham = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableSanPhamVuaThem = new javax.swing.JTable();
        jbtnLuuPhieuNhap = new javax.swing.JButton();
        jbtnDong = new javax.swing.JButton();
        jbtnInPhieuNhap = new javax.swing.JButton();
        jlblTenNhanVienLapPhieu = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setToolTipText("");
        jPanel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel1.setName(""); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Tên SP");
        jLabel2.setToolTipText("");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Danh mục SP");
        jLabel3.setToolTipText("");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Số lượng");
        jLabel4.setToolTipText("");

        jtxtSoLuong.setFont(new java.awt.Font("Tahoma", 0, 12));

        jcbxDanhMucSP.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Chọn danh mục SP]" }));
        jcbxDanhMucSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbxDanhMucSPActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Đơn vị tính");
        jLabel6.setToolTipText("");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Đơn giá");
        jLabel7.setToolTipText("");

        jbtnHuy.setText("Hủy");
        jbtnHuy.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnHuyMouseClicked(evt);
            }
        });

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("[Unknow]");

        jlblDonGiaSP.setFont(new java.awt.Font("Tahoma", 0, 12));
        jlblDonGiaSP.setText("[Unknow]");

        jlblTenSP.setFont(new java.awt.Font("Tahoma", 0, 12));
        jlblTenSP.setText("[Chọn Sản phẩm bên dưới]");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblTenSP, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(jlblDonGiaSP, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(jcbxDanhMucSP, javax.swing.GroupLayout.Alignment.TRAILING, 0, 300, Short.MAX_VALUE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtxtSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnHuy, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jLabel10))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbxDanhMucSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jlblTenSP))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(jlblDonGiaSP))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbtnHuy, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("NHẬP SẢN PHẨM");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Danh Sách Sản Phẩm Thuộc Danh Mục Trên", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));
        jPanel3.setToolTipText("");

        jTableSanPham.setAutoCreateRowSorter(true);
        jTableSanPham.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTableSanPham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableSanPham.setFillsViewportHeight(true);
        jTableSanPham.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSanPhamMouseClicked(evt);
            }
        });
        jTableSanPham.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableSanPhamKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSanPham);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Danh Sách Sản Phẩm Vừa Thêm"));
        jPanel4.setToolTipText("");

        jTableSanPhamVuaThem.setAutoCreateRowSorter(true);
        jTableSanPhamVuaThem.setFont(new java.awt.Font("Tahoma", 0, 12));
        jTableSanPhamVuaThem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableSanPhamVuaThem.setFillsViewportHeight(true);
        jScrollPane2.setViewportView(jTableSanPhamVuaThem);

        jbtnLuuPhieuNhap.setText("Lưu Phiếu Nhập");
        jbtnLuuPhieuNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLuuPhieuNhapActionPerformed(evt);
            }
        });

        jbtnDong.setText("Đóng");
        jbtnDong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDongActionPerformed(evt);
            }
        });

        jbtnInPhieuNhap.setText("In Phiếu Nhập");
        jbtnInPhieuNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnInPhieuNhapActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(323, Short.MAX_VALUE)
                .addComponent(jbtnInPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnLuuPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnDong, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnDong, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnLuuPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnInPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jlblTenNhanVienLapPhieu.setFont(new java.awt.Font("Tahoma", 0, 12));
        jlblTenNhanVienLapPhieu.setText("[Unknow]");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Nhân viên lập phiếu:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Cửa hàng");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("[Unknow]");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlblTenNhanVienLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(176, 176, 176)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addGap(223, 223, 223))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlblTenNhanVienLapPhieu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jcbxDanhMucSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbxDanhMucSPActionPerformed
// TODO add your handling code here:
    if(this.jcbxDanhMucSP.getSelectedIndex()!=0) {
        Danhmuc dm = (Danhmuc)this.jcbxDanhMucSP.getSelectedItem();
        pojoDanhMuc = dm;
        isMaDanhMucSelected = true;
        CreateTableSanPham();
    } else {
        for (int i = TableModel.getRowCount() - 1; i >= 0; i-- ){
                TableModel.removeRow(i);
        }
        jTableSanPham.setModel(TableModel);
        isMaDanhMucSelected = false;
    }
}//GEN-LAST:event_jcbxDanhMucSPActionPerformed

private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
// TODO add your handling code here:
    try {
        if(jTableSanPham.getSelectedRow()!=-1) {
            int iRow = jTableSanPham.getSelectedRow();
            String strTenSP = jTableSanPham.getModel().getValueAt(iRow, 2).toString();
            String strDonGiaSP = jTableSanPham.getModel().getValueAt(iRow, 3).toString();
            String strSoLuongCanThem = this.jtxtSoLuong.getText();
            iSoLuongSanPham = Integer.parseInt(this.jtxtSoLuong.getText());
            pojoDonViTinh = (Donvitinh)jTableSanPham.getModel().getValueAt(iRow, 5);
            pojoDanhMuc = (Danhmuc)jTableSanPham.getModel().getValueAt(iRow, 6);
            String strMaSP = jTableSanPham.getModel().getValueAt(iRow, 1).toString();
            
            pojoSanPhamVuaThem = new Sanpham(Integer.parseInt(strMaSP),pojoDanhMuc,pojoDonViTinh,strTenSP,Double.parseDouble(strDonGiaSP),null,null,null);
            //pojoSanPhamVuaThem.setDongia(dSoLuongSanPham);
            
            CreateTableSanPhamVuaThem(pojoSanPhamVuaThem, strSoLuongCanThem);
            //JOptionPane.showMessageDialog(this, "Dòng thứ: " + stt, "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Chưa chọn", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, "Số lượng phải là số, vui lòng nhập lại.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        this.jtxtSoLuong.setText("");
        this.jtxtSoLuong.requestFocusInWindow();
        return;
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Exception: " + ex.toString(), "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_jbtnThemActionPerformed

private void jbtnHuyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnHuyMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jbtnHuyMouseClicked

    private void jbtnDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDongActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtnDongActionPerformed

    private void jbtnInPhieuNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnInPhieuNhapActionPerformed
        // TODO add your handling code here:
        // JOptionPane.showMessageDialog(this, "In Phiếu", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        try {
            
            //JOptionPane.showMessageDialog(this, "In Phiếu", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, "Exception: " + ex.toString(), "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnInPhieuNhapActionPerformed

    private void jbtnLuuPhieuNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLuuPhieuNhapActionPerformed
        // TODO add your handling code here:
        // JOptionPane.showMessageDialog(this, "Lưu Phiếu", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        try {
            boolean isKiemTra = true;
            for (int i = TableModelVuaThem.getRowCount() - 1; i >= 0; i-- ){
                Integer iMaSp = Integer.parseInt(TableModelVuaThem.getValueAt(i, 1).toString());
                Sanpham sp = new Sanpham(iMaSp);
                
                Integer iSL = Integer.parseInt(TableModelVuaThem.getValueAt(i, 4).toString());
                Date dtDate = new Date();
                Double dDG = Double.parseDouble(TableModelVuaThem.getValueAt(i, 3).toString());
                
                Phieunhap pojoPhieuNhap = new Phieunhap();
                pojoPhieuNhap.setNhanvien(pojoNhanVien);
                pojoPhieuNhap.setNgaylap(dtDate);
                boolean isKQ = PhieuNhapDAO.themPhieuNhap(pojoPhieuNhap);
                
            }
            
            if (isKiemTra==true) {
                JOptionPane.showMessageDialog(this, "Lưu phiếu thành công.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "Không thành công.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
            
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, "Exception: " + ex.toString(), "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnLuuPhieuNhapActionPerformed

    private void jTableSanPhamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSanPhamMouseClicked
        // TODO add your handling code here:
        try {
            if(jTableSanPham.getSelectedRow()!=-1) {
                int iRow = jTableSanPham.getSelectedRow();
                String strTenSP = jTableSanPham.getModel().getValueAt(iRow, 1).toString();
                String strDonGiaSP = jTableSanPham.getModel().getValueAt(iRow, 2).toString();
                this.jlblTenSP.setText(strTenSP);
                this.jlblDonGiaSP.setText(strDonGiaSP);
                
                //JOptionPane.showMessageDialog(this, "Dòng thứ: " + stt, "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Exception: " + ex.toString(), "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jTableSanPhamMouseClicked

    private void jTableSanPhamKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableSanPhamKeyPressed
        // TODO add your handling code here:
//        try {
//            if(jTableSanPham.getSelectedRow()!=-1) {
//                int iRow = jTableSanPham.getSelectedRow();
//                String strTenSP = jTableSanPham.getModel().getValueAt(iRow, 1).toString();
//                String strDonGiaSP = jTableSanPham.getModel().getValueAt(iRow, 2).toString();
//                this.jlblTenSP.setText(strTenSP);
//                this.jlblDonGiaSP.setText(strDonGiaSP);
//                //JOptionPane.showMessageDialog(this, "Dòng thứ: " + stt, "Thông báo", JOptionPane.INFORMATION_MESSAGE);
//            }
//        } catch (Exception ex) {
//            JOptionPane.showMessageDialog(this, "Exception: " + ex.toString(), "Thông báo", JOptionPane.INFORMATION_MESSAGE);
//        }
    }//GEN-LAST:event_jTableSanPhamKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmNhapSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmNhapSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmNhapSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmNhapSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmNhapSanPham().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.demo.DateChooserPanel dateChooserPanel1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JDesktopPane jDesktopPane2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableSanPham;
    private javax.swing.JTable jTableSanPhamVuaThem;
    private javax.swing.JButton jbtnDong;
    private javax.swing.JButton jbtnHuy;
    private javax.swing.JButton jbtnInPhieuNhap;
    private javax.swing.JButton jbtnLuuPhieuNhap;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JComboBox jcbxDanhMucSP;
    private javax.swing.JLabel jlblDonGiaSP;
    private javax.swing.JLabel jlblTenNhanVienLapPhieu;
    private javax.swing.JLabel jlblTenSP;
    private javax.swing.JTextField jtxtSoLuong;
    // End of variables declaration//GEN-END:variables
}
